package com.key.win.business.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.key.win.base.auth.AuthenticationUtil;
import com.key.win.base.exception.BizException;
import com.key.win.business.chat.dao.ImFriendGroupDao;
import com.key.win.business.chat.model.ImFriend;
import com.key.win.business.chat.model.ImFriendGroup;
import com.key.win.business.chat.service.ImFriendGroupService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ImFriendGroupServiceImpl extends ServiceImpl<ImFriendGroupDao, ImFriendGroup> implements ImFriendGroupService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    private AbstractWrapper buildWrapper(ImFriendGroup imFriendGroup) {
        if (imFriendGroup == null) {
            logger.error("friendGroup is null!");
            throw new IllegalArgumentException("好友组为空！");
        }
        if (StringUtils.isBlank(imFriendGroup.getUserId())) {
            logger.error("userId is null!");
            throw new IllegalArgumentException("好友为空！");
        }
        LambdaQueryWrapper<ImFriendGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(imFriendGroup.getName())) {
            lambdaQueryWrapper.eq(ImFriendGroup::getName, imFriendGroup.getName());
        }
        lambdaQueryWrapper.eq(ImFriendGroup::getUserId, imFriendGroup.getUserId());
        return lambdaQueryWrapper;
    }


    @Override
    public List<ImFriend> findFriendByFriendGroup(ImFriendGroup imFriendGroup) {
        return this.list(this.buildWrapper(imFriendGroup));
    }

    @Override
    public boolean saveOrUpdateFriendGroup(ImFriendGroup imFriendGroup) {
        if (imFriendGroup == null) {
            logger.error("friendGroup is null!");
            throw new IllegalArgumentException("好友组为空！");
        }
        if (StringUtils.isBlank(imFriendGroup.getUserId())) {
            logger.error("userId is null!");
            throw new IllegalArgumentException("当前用户为空！");
        }
        if (StringUtils.isBlank(imFriendGroup.getName())) {
            logger.error("gorup name is null!");
            throw new IllegalArgumentException("分组名为空！");
        }
        ImFriendGroup po = null;
        if (StringUtils.isNotBlank(imFriendGroup.getId())) {
            po = this.getById(imFriendGroup.getId());
            po.setName(imFriendGroup.getName());
        } else {
            List<ImFriend> imFriendGroups = this.findFriendByFriendGroup(imFriendGroup);
            if (!CollectionUtils.isEmpty(imFriendGroups)) {
                logger.error("当前{}用户已经存在{}分组信息！", AuthenticationUtil.getUserName(), imFriendGroup.getName());
                throw new BizException("您已经存在" + imFriendGroup.getName() + "分组信息！");
            }
            po = imFriendGroup;
        }
        return this.saveOrUpdate(po);
    }

    @Override
    public List<ImFriend> findFriendGroupByUserId(String userId) {
        ImFriendGroup fg = new ImFriendGroup();
        fg.setUserId(userId);
        return this.findFriendByFriendGroup(fg);
    }
}
